//
//  YHIMUIKITConfig.m
//  Meow
//
//  Created by apple on 2019/2/23.
//  Copyright © 2019 yinhe. All rights reserved.
//

#import "YHIMUIKITConfig.h"

#import "YHIMUIKitSystemFaceView.h"


#define EMOJI_CODE_TO_SYMBOL(x) ((((0x808080F0 | (x & 0x3F000) >> 4) | (x & 0xFC0) << 10) | (x & 0x1C0000) << 18) | (x & 0x3F) << 24)

@interface YHIMUIKITConfig()
@property (nonatomic, strong) NSMutableArray<NSArray *> *sysFaces;
@end

@implementation YHIMUIKITConfig

+ (instancetype)sharedInstance{
    static YHIMUIKITConfig *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.sysFaces = [NSMutableArray array];
    }
    return self;
}


- (void)defaultConfig{
    
    NSMutableArray<YHIMUIKitSystemFace *> *sysFaces = [NSMutableArray array];
    for (int i = 0x1F600; i <= 0x1F64F; i++) {
        if (i < 0x1F641 || i > 0x1F644) {
            int sym = EMOJI_CODE_TO_SYMBOL(i);
            NSString *emoT = [[NSString alloc] initWithBytes:&sym length:sizeof(sym) encoding:NSUTF8StringEncoding];
            YHIMUIKitSystemFace *sysFace = [[YHIMUIKitSystemFace alloc] init];
            sysFace.sysFaceStr = emoT;
            [sysFaces addObject:sysFace];
        }
    }
    
    int sysFaceIndex = 0;
    int perItemCount = kYHIMUIKIT_SystemFace_RowCount * kYHIMUIKIT_SystemFace_PerRowCount;
    int pageCount = ceil(sysFaces.count * 1.0 / (perItemCount - 1));
    
    for (int i = 0; i < pageCount; i ++) {
        NSMutableArray *tmpAry = [NSMutableArray array];
        for (int j = 0; j < perItemCount; j ++) {
            if (j == perItemCount - 1) {
                YHIMUIKitSystemFaceDelete *delete = [[YHIMUIKitSystemFaceDelete alloc] init];
                [tmpAry addObject:delete];
            } else {
                if (sysFaceIndex <= sysFaces.count - 1) {
                    [tmpAry addObject:sysFaces[sysFaceIndex]];
                    sysFaceIndex ++;
                } else {
                    YHIMUIKitSystemFaceEmpty *empty = [[YHIMUIKitSystemFaceEmpty alloc] init];
                    [tmpAry addObject:empty];
                }
            }
        }
        [self.sysFaces addObject:tmpAry];
    }
}


// 根据表情字符串获取相应的表情
- (UIImage *)getFaceImageWith:(NSString *)faceStr{
    return nil;
}



@end

